package hub.cathome.service.impl;

import hub.cathome.entity.ResourceLink;
import hub.cathome.entity.User;
import hub.cathome.entity.tool.FileEraser;
import hub.cathome.mapper.*;
import hub.cathome.service.ShotBlogService;
import hub.cathome.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name="userMapper")
    private UserMapper userMapper;
    @Resource(name="shotBlogService")
    private ShotBlogService shotBlogService;
    @Resource(name="likeMapper")
    private LikeMapper likeMapper;
    @Resource(name="commentMapper")
    private CommentMapper commentMapper;
    @Resource(name="shotBlogMapper")
    private ShotBlogMapper shotBlogMapper;
    @Resource(name="resourceLinkMapper")
    private ResourceLinkMapper resourceLinkMapper;
    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Integer userId) {
        List<Integer> shotBlogIds = shotBlogMapper.getShotBlogIdByUserId(userId);
        for (Integer shotBlogId : shotBlogIds){
            shotBlogService.deleteShotBlog(shotBlogId);
            System.out.println("[shotBlogService] ->[delete]"+shotBlogId);
        }
        List<Integer> commentIds = commentMapper.getCommentIdByUserId(userId);
        List<Integer> likeIds = likeMapper.getLikeIdByUserId(userId);
        for(Integer commentId:commentIds){
            commentMapper.deleteComment(commentId);
            System.out.println("[commentMapper] ->[delete]"+commentId);
        }
        for(Integer likeId:likeIds){
            likeMapper.deleteLike(likeId);
            System.out.println("[likeMapper] ->[delete]"+likeId);
        }
        List<ResourceLink> resourceLinks = resourceLinkMapper.getResourceLinkByUserId(userId);
        for(ResourceLink resourceLink:resourceLinks){
            resourceLinkMapper.deleteResourceLink(resourceLink.getResourceId());
            FileEraser.erase(resourceLink);
        }
        return userMapper.deleteUser(userId);
    }

    @Override
    public User getUserByComplexName(String complexNameOrId) {
        User u = userMapper.getUserByComplexName(complexNameOrId);
        List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(u.getId());
        if(rs.size()>0){
            u.setIcon(rs.get(0));
        }
        return u;
    }

    @Override
    public int getUserCountByComplexName(String complexName) {
        return userMapper.getUserCountByComplexName(complexName);
    }


    @Override
    public User getUserByUserId(Integer userId) {
        User u = userMapper.getUserById(userId);;
        List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(u.getId());
        if(rs.size()>0){
            u.setIcon(rs.get(0));
        }
        return u;
    }

    @Override
    public int login(@Nullable String complexNameOrId,@Nullable Long password) {
        if(complexNameOrId==null||password==null){
            return -1;
        }
        User user = getUserByComplexName(complexNameOrId);
        if(user==null){
            return -1;
        }
        if(!Objects.equals(user.getPassword(), password)){
            return -1;
        }
        if(Objects.equals(user.getUsername(), complexNameOrId)){
            return 1;
        }
        if(Objects.equals(user.getNickname(), complexNameOrId)){
            return 1;
        }
        return -1;
    }

    @Override
    public List<User> getUserByListId(List<Integer> id) {
        List<User> users = userMapper.getUserByListId(id);;
        for(User user:users){
            List<ResourceLink> rs = resourceLinkMapper.getResourceLinkByUserId(user.getId());
            if(rs.size()>0){
                user.setIcon(rs.get(0));
            }
        }
        return users;
    }
}
